package com.puhui.education.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.puhui.education.R;
import com.zhy.android.percent.support.PercentLinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/5/27.
 */
public class SearchView extends LinearLayout {

    @Bind(R.id.iv_leftImg_searchViewCv)
    ImageView iv_leftImg;
    @Bind(R.id.et_searchContent_searchViewCv)
    EditText et_searchContent;
    @Bind(R.id.pll_searchViewCv)
    PercentLinearLayout pll;

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSearchView(context);
        initAttrs(context, attrs);
    }

    private void initSearchView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.cv_search, this);
        ButterKnife.bind(view);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SearchView);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.SearchView_leftImg:
                    iv_leftImg.setImageResource(a.getResourceId(attr, -1));
                    break;
                case R.styleable.SearchView_sv_hintText:
                    et_searchContent.setHint(a.getString(attr));
                    break;
                case R.styleable.SearchView_barBackground:
                    pll.setBackgroundResource(a.getResourceId(attr, -1));
                    break;
                case R.styleable.SearchView_sv_editTextColor:
                    et_searchContent.setTextColor(a.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.SearchView_isCursorVisible:
                    et_searchContent.setCursorVisible(a.getBoolean(attr, true));
                    break;
                case R.styleable.SearchView_hintTextColor:
                    et_searchContent.setHintTextColor(a.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.SearchView_isEditable:
                    if (!a.getBoolean(attr, true)) {
                        et_searchContent.setInputType(InputType.TYPE_NULL);
                    }
                    break;
            }
        }
        a.recycle();
    }

    //监听输入内容变化
    public void onEditContenttChangeListener(TextWatcher textWatcher) {
        et_searchContent.addTextChangedListener(textWatcher);
    }
}
